package geppetto.phraseTable.phrase.feature.global;

import java.util.LinkedList;

import geppetto.decoder.TranslationHypothesis;
import geppetto.decoder.scores.GraphPhraseProductScore;
import geppetto.decoder.scores.NumberOfOptionsScore;
import geppetto.phraseTable.PhraseTable;
import geppetto.phraseTable.phrase.Phrase;
import geppetto.phraseTable.phrase.feature.LexicalWeightingFeature;
import geppetto.phraseTable.phrase.feature.ProbabilityFeature;
import geppetto.phraseTable.resource.TranslationGraphResource;

public class PhraseBasedLexicalWeightingAverageCalc implements AbstractGlobalFeatureCalc {
	protected final static String FEATURE = "pavgweight";
	protected final static String FEATURE_2 = "ravgweight";
	protected TranslationGraphResource translationGraph;
	protected boolean reverse = false;
	protected int order;
	
	public PhraseBasedLexicalWeightingAverageCalc(int order,
			TranslationGraphResource translationGraph, boolean reverse) {
		super();
		this.translationGraph = translationGraph;
		this.order = order;
		this.reverse = reverse;
	}

	@Override
	public void calculateFeature(Phrase phrase, PhraseTable table) {
		LinkedList<TranslationHypothesis> hyps = translationGraph.getFinalHyps();
		GraphPhraseProductScore pweightingScore = (GraphPhraseProductScore)translationGraph.getScore(TranslationGraphResource.PHRASE_WEIGHTING);
		GraphPhraseProductScore rpweightingScore = (GraphPhraseProductScore)translationGraph.getScore(TranslationGraphResource.REVERSE_PHRASE_WEIGHTING);
		double numberOfLongestPaths = 0;
		double pweight = 0;
		double rpweight = 0;
		for(TranslationHypothesis h : hyps){
				pweight += pweightingScore.getScore(h);
				rpweight += rpweightingScore.getScore(h);
				numberOfLongestPaths++;
		}
		
		if(reverse){
			phrase._features.addFeature(FEATURE_2, new LexicalWeightingFeature(rpweight/numberOfLongestPaths), order);			
		}
		else{
			phrase._features.addFeature(FEATURE, new LexicalWeightingFeature(pweight/numberOfLongestPaths), order);
		}
	}

}
